package inet.app.vod.video;

import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.widget.Toast;
import inet.app.vod.activity.R;
import inet.app.vod.util.MyPreferenceManager;
import inet.app.vod.video.MyMediaController.MediaPlayerControl;

/**
 * MediaController with function, no UI for media controller gesture
 * @author thangdlv
 *
 */
public class MyMediaControllerBase {
	
	public static final int TYPE_GESTURE = 0;
	public static final int TYPE_BUTTON  = 1;
	
	private static final int SPACE_VOLUME = 10;
	private static final int MAX_VOLUME   = 100;
	private static final int MIN_VOLUME   = 0;
	
	private MediaPlayerControl mPlayer;
	private Context mContext;
	private Resources mResources;
	private AudioManager mAM;
	
	public int mVolumeLevelCurrent;
	private boolean mIsMute;
	
	//Scale video type
	private int[] mTypeScale = new int[] {  MyVideoView.VIDEO_LAYOUT_ORIGIN
										  , MyVideoView.VIDEO_LAYOUT_SCALE
										  , MyVideoView.VIDEO_LAYOUT_SCALE
										  , MyVideoView.VIDEO_LAYOUT_STRETCH };
	int mCurrentScale = MyVideoView.VIDEO_LAYOUT_STRETCH;
	
	public MyMediaControllerBase(MediaPlayerControl player, Context context) {
		this.mPlayer = player;
		this.mContext = context;
		this.mResources = mContext.getResources();
		
		mAM = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

		// Load volume from preference
		mVolumeLevelCurrent = MyPreferenceManager.getInstance(context).getVolume();
	}
	
	public void pause() {
		if(null != mPlayer) {
			if(mPlayer.isPlaying()) {
				mPlayer.pause();
			}
		}	
	}
	
	public void play() {
		if(null != mPlayer) {
			if(!mPlayer.isPlaying()) {
				mPlayer.start();
			}
		}
	}
	
	public void capture() {
		if(null != mPlayer) {
			mPlayer.onCapture();
		}
	}
	
	public void plusVolume() {
		if(mVolumeLevelCurrent == MAX_VOLUME) {
			Toast.makeText(mContext, mResources.getString(R.string.volume_da_la_lon_nhat), 
					Toast.LENGTH_SHORT).show();
			return;
		}
		mVolumeLevelCurrent += SPACE_VOLUME;
		mAM.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeLevelCurrent, 0);
	}
	
	public void minusVolume() {
		if(mVolumeLevelCurrent == MIN_VOLUME) {
			Toast.makeText(mContext, mResources.getString(R.string.volume_da_la_nho_nhat), 
					Toast.LENGTH_SHORT).show();
			return;
		}
		mVolumeLevelCurrent -= SPACE_VOLUME;
		mAM.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeLevelCurrent, 0);
	}
	
	public void muteVolume() {
		if(!mIsMute) {
			mAM.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
			mIsMute = true;
		}
		else {
			mAM.setStreamVolume(AudioManager.STREAM_MUSIC,
					mVolumeLevelCurrent, 0);
			mIsMute = false;
		}
	}
	
	public void scale() {
		mCurrentScale = (mCurrentScale + 1) % mTypeScale.length;
		mPlayer.onScale(mCurrentScale);
	}
	
	public int getCurrentScale() {
		  return mCurrentScale;
	}

}
